package cn.wolfcode.web.controller;

import cn.wolfcode.annotation.PermissionName;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.TableDataInfo;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.TableQueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.FileInputStream;
import java.io.IOException;


/**
 * ClassName DepartmentController
 * Create by Mike
 * Date 2021/8/1 16:52
 */
@Controller
@RequestMapping("/department")
public class DepartmentController  {

    @Autowired
    private IDepartmentService departmentService;

    @PermissionName("部门查询")
    @RequiresPermissions("department:list")
    @RequestMapping("/list")
    public String list(){
        return "/department/list";
    }


    @PermissionName("部门新增/修改")
    @RequiresRoles("CEO")
    @RequiresPermissions("department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        if (department.getId() == null) { // 新增
            departmentService.save(department);
        } else {
            departmentService.update(department);
        }
        return new JsonResult(true, "操作成功");
    }


    @RequestMapping("/delete")
    @PermissionName("部门删除")
    @RequiresPermissions("department:delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            departmentService.delete(id);
        }
        return new JsonResult(true, "删除成功");
    }


    @RequestMapping("/listData")
    @ResponseBody
    public TableDataInfo<Department> listData(TableQueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.query(qo);
        return new TableDataInfo<>(pageInfo.getTotal(), pageInfo.getList());
    }


    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        Workbook workbook = departmentService.export();
        response.setHeader("Content-disposition","attachment;filename=department.xls");
        workbook.write(response.getOutputStream());
    }

    @Value("${download.dir}")
    private String downloadDir;

    @RequestMapping("/downloadTemplate")
    public void downloadTemplates(HttpServletResponse response) throws IOException{
        FileInputStream inputStream = new FileInputStream( downloadDir + "/department.xls");
        response.setHeader("Content-disposition","attachment;filename=department.xls");
        FileCopyUtils.copy(inputStream,response.getOutputStream());
    }

    @RequestMapping("/importExcel")
    @ResponseBody
    public JsonResult importExcel(Part file) throws IOException{
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        try {
            int count = departmentService.importExcel(workbook);
            return new JsonResult(true,"导入" + count + "条");
        } catch (Exception e) {
            return new JsonResult(false,"导入失败");
        }
    }

}
